package com.topisv.tms.web.work;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springside.modules.web.Servlets;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.work.WorkTask;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.work.WorkTaskService;

/**
 * 单据上架任务_control
 * @author ch
 */
@Controller
@RequestMapping(value = "/billup")
public class BillupController {

	private static final Logger logger = LoggerFactory.getLogger(BillupController.class);

	@Autowired
	private WorkTaskService workTaskService;
	@Autowired
	private CustomersService customersService;
	@Autowired
	private AreaGroupService areaGroupService;
	
	/**
	 * 导向单据上架页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toBillupPage")
	public String toBillupPage(ServletRequest request,HttpServletResponse response,Model model){
		try {
			
		    ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		    String customerCode=curUser.customerCode;
		    Customers customer=this.customersService.findByCustomerCode(customerCode);
		    model.addAttribute("customer", customer); 	
		    
	
		    List<AreaGroup> wmList=this.areaGroupService.loadByCustomerCode(customerCode);
			model.addAttribute("wmList", wmList);
			
		    
		    //查找我的单据上架任务
		    Map<String, Object> param=new HashMap<String, Object>();
		    param.put("printStatus", "printed");
		    param.put("customerCode", customerCode);
		    param.put("flowType", "prePrint");
		    param.put("pickType", "multi");
		    param.put("billStatus", "noup");
		    param.put("uperId", curUser.id);
		    
		    List<WorkTask> myBillUpTaskList=this.workTaskService.loadTaskByParam(param);
		    model.addAttribute("myBillUpTaskList",myBillUpTaskList);
		    
		    
		    //查找未分配的单据上架任务
	    
	    
		} catch (Exception e) {
			
			logger.error("error",e);
		}
	    
		return "work/billup/billupList";
	}
	
	/**
	 * 查询单据上架任务
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "queryBillupList")
	public String queryBillupList(Model model, ServletRequest request, ServletResponse response) throws Exception {
		
		try {
			
			ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		    String customerCode=curUser.customerCode;
		    Customers customer=this.customersService.findByCustomerCode(customerCode);
		    model.addAttribute("customer", customer); 
			
			List<AreaGroup> wmList=this.areaGroupService.loadByCustomerCode(customerCode);
			model.addAttribute("wmList", wmList);
			
		    
		    //查询未分配的拣货任务
			Map<String, Object> param = Servlets.getParametersStartingWith(request, "_");
			String fromDateStr=(String) param.get("fromDate");
			String toDateStr=(String) param.get("toDate");
			
			Date fromDate=null;
			Date toDate=null;
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			if(StringUtils.isNotEmpty(fromDateStr)){
				fromDate=sdf.parse(fromDateStr+" 00:00:00");
			}
			if(StringUtils.isNotEmpty(toDateStr)){
				toDate=sdf.parse(toDateStr+" 23:59:59");
			}
			
			param.put("fromDate", fromDate);
			param.put("toDate", toDate);
			param.put("customerCode", customerCode);
		    param.put("flowType", "prePrint");
			
			List<WorkTask> taskList=this.workTaskService.loadTaskByParam(param);
		    model.addAttribute("taskList",taskList);
		    
		    model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(param, "_"));
			
			} catch (Exception e) {
				
				logger.error("error",e);
			}

		return "work/billup/billupList";
	}
	
	
}